package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.mwsreportbean.MwsFbaCustomerReturns;
import com.yunchang.springboot.models.mwsreportbean.MwsFbaCustomerReturnsDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:46
 * @description:
 */
@Component
public class FbaCustomerReturnsDataDispose extends ReportDataDisposeParent implements IReportDataDispose {
    /**
     * 退货 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
//        String latestDay = mwsFbaCustomerReturnsMapper.getLatestDay(sellerId, area);
        int returnDateIndex = Integer.valueOf(String.valueOf(titleMap.get("return-date")));
//        if (!StringUtil.isBlank(latestDay)) {
//            //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//            if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(lists.get(1).get(returnDateIndex))) >= 0) {
//                return null;
//            }
//        }
        int orderIdIndex = Integer.valueOf(String.valueOf(titleMap.get("order-id")));
        int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
        int asinIndex = Integer.valueOf(String.valueOf(titleMap.get("asin")));
        int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
        int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
        int quantityIndex = Integer.valueOf(String.valueOf(titleMap.get("quantity")));
        int fulfillmentCenterIdIndex = Integer.valueOf(String.valueOf(titleMap.get("fulfillment-center-id")));
        int detailedDispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("detailed-disposition")));
        int reasonIndex = Integer.valueOf(String.valueOf(titleMap.get("reason")));
        int statusIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("status")) {
            statusIndex = Integer.valueOf(String.valueOf(titleMap.get("status")));
        }
        int licensePlateNumberIndex = Integer.valueOf(String.valueOf(titleMap.get("license-plate-number")));
        int customerCommentsIndex = Integer.valueOf(String.valueOf(titleMap.get("customer-comments")));
        List<Object> list = new ArrayList<>(lists.size());
        for (int i = 1; i < lists.size(); i++) {
            MwsFbaCustomerReturnsDO mwsFbaCustomerReturns = new MwsFbaCustomerReturnsDO();
            List<String> stringList = lists.get(i);
            mwsFbaCustomerReturns.setSellerId(sellerId);
            mwsFbaCustomerReturns.setArea(area);
            mwsFbaCustomerReturns.setReturnDate(DateUtil.ISO8601ToGMT(stringList.get(returnDateIndex)));
            mwsFbaCustomerReturns.setOrderId(stringList.get(orderIdIndex));
            mwsFbaCustomerReturns.setSku(stringList.get(skuIndex));
            mwsFbaCustomerReturns.setAsin(stringList.get(asinIndex));
            mwsFbaCustomerReturns.setFnsku(stringList.get(fnskuIndex));
            mwsFbaCustomerReturns.setProductName(stringList.get(productNameIndex));
            mwsFbaCustomerReturns.setQuantity(ToolUtil.valueOfInteger(stringList.get(quantityIndex)));
            mwsFbaCustomerReturns.setFulfillmentCenterId(stringList.get(fulfillmentCenterIdIndex));
            mwsFbaCustomerReturns.setDetailedDisposition(stringList.get(detailedDispositionIndex));
            mwsFbaCustomerReturns.setReason(stringList.get(reasonIndex));
            if (statusIndex != WRONG_NUMBER_SIGN) {
                mwsFbaCustomerReturns.setStatus(stringList.get(statusIndex));
            }
            mwsFbaCustomerReturns.setLicensePlateNumber(stringList.get(licensePlateNumberIndex));
            mwsFbaCustomerReturns.setCustomerComments(stringList.get(customerCommentsIndex));
            mwsFbaCustomerReturns.setCreateTime(DateUtil.getDate());
            mwsFbaCustomerReturns.setUpdateTime(DateUtil.getDate());
            mwsFbaCustomerReturns.setFinanceStatus(1);
            list.add(mwsFbaCustomerReturns);
        }
        return list;
    }
}
